package shop.controller.front.address;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.address.AddressService;
import shop.services.front.address.bean.Address;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhuangjunfeng on 2017/6/3.
 */
@Controller
@RequestMapping("/front/address")
public class AddressController extends FrontBaseController<Address> {
    @Resource
    private AddressService addressService;
    @Override
    public Services<Address> getService() {
        return addressService;
    }

    /**
     * 根据用户ID查询用户地址列表
     * @param address 用户地址
     * @return 用户地址列表
     */
    @RequestMapping(value = "/selectAddressList",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectAddressList(Address address){
        JSONResult rs=new JSONResult();
        List<Address> addressList=addressService.selectList(address);
        rs.setData(addressList);
        rs.setSuccess(true);
        return rs;
    }

    /**
     * 新增用户地址
     * @param address 用户地址
     * @return 是否成功
     */
    @RequestMapping(value = "/insertAddress",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult insertAdress(Address address){
        JSONResult rs =new JSONResult();
        addressService.insert(address);
        rs.setSuccess(true);
        return  rs;
    }

    /**
     * 批量更新地址
     * @param address 地址
     * @return 是否成功
     */
    @RequestMapping(value = "/updateAddress",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult updateAdress(Address address){
        JSONResult rs =new JSONResult();
        addressService.update(address);
        rs.setSuccess(true);
        return  rs;
    }

    /**
     * 查询默认地址
     * @param address
     * @return
     */
    @RequestMapping(value ="/selectDefaultAddress",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectDefaultAddress (Address address){
        JSONResult rs=new JSONResult();
       Address defaultAddress= addressService.selectDefaultAddress(address);
        rs.setData(defaultAddress);
        rs.setSuccess(true);
        return rs;
    }
    @RequestMapping(value = "/setDefaultAddress",method = RequestMethod.POST)
    @ResponseBody
    public JSONResult setDefaultAddress(Address address){
        JSONResult rs=new JSONResult();
        addressService.setAddressDefault(address);
        rs.setSuccess(true);
        return rs;
    }
    /**
     * 查询单个地址
     * @param address 地址
     */
    @RequestMapping(value = "/selectOne",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectOne(Address address){
        JSONResult rs=new JSONResult();
        rs.setData(addressService.selectOne(address));
        rs.setSuccess(true);
        return rs;
    }

    /**
     * 根据id删除地址
     * @param address 地址
     */
    @RequestMapping(value = "/deleteAddress",method = RequestMethod.GET)
    @ResponseBody
    public  JSONResult deleteAddress(Address address){
        JSONResult rs=new JSONResult();
        addressService.delete(address);
        rs.setSuccess(true);
        return rs;
    }
}
